#!/bin/bash

function success {
	echo -e "\e[00;32m$1\e[00m"
}

function alert {
	echo -e "\e[00;33m$1\e[00m"
}

function error {
	echo -e "\e[00;31m$1\e[00m"
}

function is_installed {
        if which $1>/dev/null; then
                return 0
        fi

        return 1
}

function validate_email {
        RESULT=$(echo $1 | grep '^[a-zA-Z0-9._%+-]*@[a-zA-Z0-9]*[\.[a-zA-Z0-9]*]*[a-zA-Z0-9]$')

        if [ "$RESULT" == "" ]; then
                return 1
        fi

        return 0
}

function validate_input {
	if [ ${#1} -lt 6 ]; then
		return 1
	elif [ ${#1} -gt 16 ]; then
		return 1
	elif [ "$(echo $1 | grep '^[a-z0-9]*$')" == "" ]; then
		return 1
	fi
        return 0
}

function install {
	PKCG=$1
	shift
	while [ -n "$1" ]
	do
		DEBIAN_FRONTEND=noninteractive apt-get -qy install "$1"
		success "$PKCG installed successfully."
		shift
	done
}

function remove {
    DEBIAN_FRONTEND=noninteractive apt-get -qy remove --purge "$1"
    success "$1 removed successfully."
}

##### Start of program #####
# Make sure script is running under root user
echo -n "Check root user "
if [ "$(id -u)" != "0" ]; then
	error "[fail]"
        echo "This script must run under root user."
	exit 0
else
	success "[pass]"
fi

# Make sure running this script under Debian based Linux
echo -n "Check Linux distribution "
if [ ! -f /etc/debian_version ]; then
	error "[fail]"
	echo "This script run under Debian distribution only."
	exit 0
else
	success "[pass]"
fi

echo -n "Please enter SKCP username: "
read USERNAME

while ! validate_input $USERNAME
do
	error "Username must to 6-16 alphanumeric characters."
	echo -n "SKCP username: "
	read USERNAME
done
success "Username \"$USERNAME\" accepted."

echo -n "Please enter SKCP password: "
read PASSWORD

while ! validate_input $PASSWORD
do
	error "Password must to 6-16 alphanumeric characters."
	echo -n "SKCP password: "
	read PASSWORD
done
success "Password accepted."

echo -n "Please enter your e-mail address: "
read EMAIL

while ! validate_email $EMAIL
do
	error "Please provide a valid e-mail address."
	echo -n "E-mail address: "
	read EMAIL
done
success "E-mail address \"$EMAIL\" accepted."

echo "Starting SKCP installation..."

# Remove sendmail as we are going to install exim4 later
if [ -f /usr/lib/sm.bin/smtpd ]; then
	invoke-rc.d sendmail stop
	remove /usr/lib/sm.bin/smtpd 'sendmail*'
fi

# Update system repositories
echo "Performing repositories update and upgrade..."
apt-get -fqy update && apt-get -fqy upgrade
success "Update and upgrade done."

# Install Apache, PHP, MySQL and ProFTPD
echo "Installing Apache, PHP, MySQL and ProFTPD..."
install apache2 apache2-mpm-prefork apache2-utils libexpat1 ssl-cert 
install libapache2-mod-php5 php5 php5-common php5-curl php5-gd php-pear php5-imagick php5-imap php5-mcrypt php5-mysql php-apc
install mysql-server mysql-client
install proftpd-basic
echo "Apache, PHP, MySQL and ProFTPD installed."

# Prevent FTP user to access Shells
echo "/bin/false" >> /etc/shells

# Remove Apache default config
echo "Reconfigure Apache settings "
rm /etc/apache2/sites-enabled/000-default
touch /etc/apache2/sites-enabled/000-default
echo '<VirtualHost *:80>
	ServerAdmin '$EMAIL'
	
	DocumentRoot /var/www/
	<Directory />
		Options FollowSymLinks
		AllowOverride None
	</Directory>
	<Directory /var/www/>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride None
		Order allow,deny
		allow from all
	</Directory>
	ErrorLog /var/log/apache2/error.log

	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel warn

	CustomLog /var/log/apache2/access.log combined
</VirtualHost>' > /etc/apache2/sites-enabled/000-default
success "[done]"

#Copy www files
echo Copying files...
cp -r www/ /var
echo Copying files done

# remove default index file
echo Removing default index file...
rm /var/www/index.html
echo Removing default index file done

# Make needed dirs
echo Creating dirs... 
mkdir /var/users
mkdir /var/users/logs
echo Creating dirs done
